package sunwin.yoms.web.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sunwin.yoms.comm.IPAddressHelper;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsFunction;
import sunwin.yoms.domain.system.FireWallDomain;
import sunwin.yoms.service.person.UserService;
import sunwin.yoms.service.security.YomsUserDetails;
import sunwin.yoms.service.system.FireWallService;
import sunwin.yoms.web.comm.R;
import sunwin.yoms.web.comm.YomsController;
import sunwin.yoms.web.comm.YomsControllerDefaultData;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by mt on 2015/7/23.
 */
@Controller
public class DefaultController {

    @Autowired
    private UserService userService;
    @Autowired
    private YomsControllerDefaultData defaultData;
    @Autowired
    private FireWallService fireWallService;

    private final String login = "login";
    private final String index = "default";
    private final String error403 = "error/403";

    @RequestMapping(value = R.Index, method = RequestMethod.GET)
    @YomsController(viewName = index)
    public ModelAndView index(HttpServletRequest request) {
        YomsUserDetails details = defaultData.getCurrentYomsUserDetails();
        ModelAndView mav = new ModelAndView();
        mav.addObject("User", details);
        return mav;
    }


    @RequestMapping(value = R.Login, method = RequestMethod.GET)
    public String login(HttpServletRequest request, IPAddressHelper IPAddressHelper) throws YomsException {
        YomsUserDetails details = defaultData.getCurrentYomsUserDetails();
        if (details != null) {
            return index;
        } else {
            //2、获取客户端IP
            String clientIp = IPAddressHelper.getIpAddr(request);
            Logger.getLogger(this.getClass()).info("访问后台登录页面IP地址：" + clientIp);
            //1、获取数据库防火墙列表里面的IP
            FireWallDomain fireWallDomain = new FireWallDomain();
            List<FireWallDomain> domains = fireWallService.getFireList(fireWallDomain);
            for (int i = 0; i < domains.size(); i++) {
                FireWallDomain domain = domains.get(i);
                if (domain.getFirePass() == 1) {
                    if (clientIp.equals(domain.getFireIp())) {
                        return error403;
                    }
                    else {
                        return error403;
                    }
                }
            }
            for (int i = 0; i < domains.size(); i++) {
                if (domains.get(i).getFirePass() == 2) {
                    if (clientIp.equals(domains.get(i).getFireIp())) {
                        return login;
                    }
                }
            }
            return error403;
        }

    }


    @RequestMapping(value = "403", method = RequestMethod.GET)
    public String error403() {
        return "error/403";
    }

    @RequestMapping(value = "404", method = RequestMethod.GET)
    public String error404() {
        return "error/404";
    }

    @RequestMapping(value = R.LoginError, method = RequestMethod.GET)
    public String loginError(ModelMap map) {
        map.addAttribute("error", "登录失败，请检查用户密码是否正确");
        return login;
    }

    /**
     * 宣传管理模块导航
     */
    @RequestMapping(value = R.IndexPublicity, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.Index, viewName = "default.publicity")
    public void indexPublicty() {
    }

    /**
     * 预定管理模块导航
     */
    @RequestMapping(value = R.IndexSubsc, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.Index, viewName = "default.subsc")
    public void indexSubscribe() {
    }

    /**
     * 人员管理模块导航
     */
    @RequestMapping(value = R.IndexUser, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.Index, viewName = "default.user")
    public void indexUser() {
    }

    /**
     * 网站设置模块导航
     */
    @RequestMapping(value = R.IndexSetting, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.Index, viewName = "default.setting")
    public void indexSetting() {
    }

    /**
     * 保活请求设置
     */
    @RequestMapping(value = R.KeepAlive, method = RequestMethod.POST)
    @ResponseBody
    public String keepAlive() {
        return "30s请求一次服务器成功";
    }

}